package com.gdiss.www.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdiss.www.bo.UserLoginBo;
import com.gdiss.www.constant.FormatConstant;
import com.gdiss.www.entity.User;
import com.gdiss.www.handler.GDUTException;
import com.gdiss.www.mapper.UserMapper;
import com.gdiss.www.service.TokenService;
import com.gdiss.www.service.UserService;
import com.gdiss.www.vo.request.RegisterVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @author Velore
 * @date 2022/1/20
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    UserService userService;

    @Resource
    TokenService tokenService;

    @Override
    public String login(UserLoginBo userLoginBo) {
        String identifier = userLoginBo.getIdentifier();
        User user = null;
        //通过正则匹配输入的账号来区别登录方式
        if(FormatConstant.EMAIL.matches(identifier)){
            //邮箱登录
            user = userService.queryUserByUserEmailAddress(identifier);
        }else if(FormatConstant.PHONE_NUMBER.matches(identifier)){
            //手机号登录
            user = userService.queryUserByUserPhoneNumber(identifier);
        }else {
            //TODO 微信登录
            //待完善
//            user = userService.queryUserByWeChatOpenid(identifier);
        }
        if(user == null){
            throw new RuntimeException("帐号不存在");
        }
        if(!user.getUserPassword().equals(userLoginBo.getPassword())){
            throw new RuntimeException("密码错误");
        }
        return tokenService.generate(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateUser(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", user.getId());
        return baseMapper.update(user, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteUser(String id) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        User user = baseMapper.selectOne(wrapper);
        return baseMapper.update(user, wrapper);
    }

    @Override
    public User queryUserById(int id) {
        return baseMapper.selectById(id);
    }

    @Override
    public User queryUserByUserPhoneNumber(String userPhoneNumber) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_phone_number", userPhoneNumber);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public User queryUserByUserEmailAddress(String userEmailAddress) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_email_address", userEmailAddress);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public User queryUserByWeChatOpenid(String userWeChatOpenId) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_wechat_openid", userWeChatOpenId);
        return baseMapper.selectOne(wrapper);
    }

    /**
     * 注册用户
     * 
     * @param registerVo 注册用户所需Vo类
     */
    @Override
    public void register(RegisterVo registerVo) {
        String username = registerVo.getUsername();
        String password = registerVo.getPassword();
        String phoneNum = registerVo.getPhoneNum();
        String verifyCode = registerVo.getVerifyCode();

        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)||StringUtils.isEmpty(phoneNum)||StringUtils.isEmpty(verifyCode)){
            throw new GDUTException(2001,"注册参数缺失");
        }

        //TODO: 短信验证服务
        if(Integer.parseInt(verifyCode)!=5888){
            throw new GDUTException(2002,"验证码不正确");
        }

        User user = new User();
        user.setUserName(username);
        user.setUserPassword(password);
        user.setUserPhoneNumber(phoneNum);
        baseMapper.insert(user);
    }
}
